//
//  FoodStoreItemModel.swift
//  FoodStore
//
//  Created by youwei on 2025/6/21.
//

import Foundation

/// 食物条目
struct FoodItemModel: Identifiable {
    let id: Int
    let imgUrl: String
    let title: String
    let price: String
    let tags: [String]
    
    init(id: Int, imgUrl: String, title: String, price: String, tags: [String] = []) {
        self.id = id
        self.imgUrl = imgUrl
        self.title = title
        self.price = price
        self.tags = tags
    }
    
    init(respModel: FoodItemRespModel) {
        self.id = respModel.id
        self.imgUrl = respModel.picFile.url
        self.title = respModel.name
        self.price = respModel.price
        self.tags = respModel.tags.split(separator: ",").map({ String($0) })
    }
}

/// 商家条目
struct FoodStoreItemModel {
    let id: Int
    /// 店铺图片
    let storeImgUrl: String
    /// 店铺名称
    let title: String
    /// 是否入驻
    let isEntry: Bool
    /// 评分
    let points: Float
    /// 评分人数
    let pointsPeopleNum: Int
    /// 距离
    let distance: String
    /// 食物
    let foods: [FoodItemModel]
    /// 标签
    let tags: [String]
    /// 营业时间
    let openTime: String
    /// 均价
    let prPrice: String
    /// 印尼语地址
    let address: String
    /// 店铺宣传图
    let scrollImgUrls: [String]
    /// 店铺中文名
    let cnName: String
    /// 入驻时间
    let entryTime: String
    /// 中文地址
    let cnAddress: String
    /// 联系人
    let contacter: String
    /// 提示
    let snapshot: String
    /// 地图
    let mapUrl: String
    /// 电话
    let phoneNum: String
    
    init(id: Int, storeImgUrl: String, title: String, isEntry: Bool, points: Float, pointsPeopleNum: Int, distance: String, foods: [FoodItemModel], tags: [String], openTime: String, prPrice: String, address: String, scrollImgUrls: [String], cnName: String, entryTime: String, cnAddress: String, contacter: String, snapshot: String, mapUrl: String, phoneNum: String) {
        self.id = id
        self.storeImgUrl = storeImgUrl
        self.title = title
        self.isEntry = isEntry
        self.points = points
        self.pointsPeopleNum = pointsPeopleNum
        self.distance = distance
        self.foods = foods
        self.tags = tags
        self.openTime = openTime
        self.prPrice = prPrice
        self.address = address
        self.scrollImgUrls = scrollImgUrls
        self.cnName = cnName
        self.entryTime = entryTime
        self.cnAddress = cnAddress
        self.contacter = contacter
        self.snapshot = snapshot
        self.mapUrl = mapUrl
        self.phoneNum = phoneNum
    }
    
    init(respModel: FoodStoreItemRespModel) {
        self.id = respModel.id
        self.storeImgUrl = respModel.logoFile?.url ?? ""
        self.title = respModel.name
        self.isEntry = respModel.settled == 1
        
        self.points = respModel.score ?? 5.0
        self.pointsPeopleNum = respModel.scoreNum ?? 9999
        
        let rd = respModel.distance ?? ""
        if let doubleRD = Double(rd) {
            self.distance = String(format: "%.2f", doubleRD)
        } else {
            self.distance = ""
        }
        
        self.foods = respModel.goods?.map({
            FoodItemModel(respModel: $0)
        }) ?? []
        self.tags = respModel.tags.split(separator: ",").map({ String($0) })
        self.openTime = respModel.businessTime
        self.prPrice = respModel.averagePrice
        self.address = respModel.indonesiaAddress
        self.scrollImgUrls = respModel.environmentPicFile?.map({ $0.url }) ?? []
        self.cnName = respModel.chineseName
        self.entryTime = respModel.settledTime
        self.cnAddress = respModel.chineseAddress
        self.contacter = respModel.contacts
        self.snapshot = respModel.snapshot
        self.mapUrl = respModel.addressGoogle
        self.phoneNum = respModel.phone
    }
    
}

